package com.mmall.shiro;

import com.mmall.model.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

/**
 * function:登录用户相关数据. <br/>
 *
 * @author weiguo.liu
 * @date:2016年5月13日 下午1:40:38 <br/>
 * @version:
 * @since:JDK 1.7
 */
public class SessionUtils {
    /**
     * 获取当前用户对象shiroUser.
     *
     * @return
     */
    public static MyShiroRealm.ShiroUser getCurrentShiroUser() {
        MyShiroRealm.ShiroUser user = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        return user;
    }

    /**
     * 获取当前用户session.
     *
     * @return session
     */
    public static Session getSession() {
        Session session = SecurityUtils.getSubject().getSession();
        return session;
    }


    /**
     * 获取当前用户对象.
     *
     * @return user
     */
    public static SysUser getCurrentUser() {
        Session session = SecurityUtils.getSubject().getSession();
        if (null != session) {
            return (SysUser) session.getAttribute(SessionConsts.SESSION_USER_KEY);
        } else {
            return null;
        }
    }

    /**
     * 获取当前登录用户id.
     *
     * @return
     */
    public static Integer getCurrentUserId() {
        SysUser user = getCurrentUser();
        if (user != null) {
            return user.getId();
        }
        return null;
    }

    /**
     * 获取当前登录用户名.
     *
     * @return
     */
    public static String getCurrentUserName() {
        SysUser user = getCurrentUser();
        if (user != null) {
            return user.getUsername();
        }
        return null;
    }
}
